const db = require("../db/index");
// 获取
exports.getports = (req, res) => {
  const sql = `select * from ev_post where is_delete=0 order by id asc `;
  db.query(sql, (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err);

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: "获取招聘信息成功！",
      data: results,
      total: results.length,
    });
  });
};
// 添加
exports.addPort = (req, res) => {
  const stuInfo = {
    ...req.body,
    date: new Date(),
  };
  const sql = `insert into ev_post set ?`;
  db.query(sql, stuInfo, (err, results) => {
    if (err) return res.cc(err);
    res.send({ status: 0, message: "新增招聘信息数据成功!" });
  });
};
// 删除
exports.delPort = (req, res) => {
  const sql = `update ev_post set is_delete = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除招聘信息数据失败!");
    res.send({ status: 0, message: "删除招聘信息数据成功!" });
  });
};
// 更新
exports.updatePort = (req, res) => {
  const sql = `update ev_post set ? where id=?`;
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("更新招聘信息数据失败!");

    res.send({ status: 0, message: "更新招聘信息数据成功!" });
  });
};
// 收藏
exports.updateState = (req, res) => {
  const sql = `update ev_post set post_state = 0 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("收藏数据失败!");
    res.send({ status: 0, message: "收藏数据成功" });
  });
};
// 取消收藏
exports.updateNoState = (req, res) => {
  const sql = `update ev_post set post_state = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("取消收藏数据失败!");
    res.send({ status: 0, message: "取消收藏数据成功" });
  });
};
// 获取收藏数据
exports.getState = (req, res) => {
  const sql = `select * from ev_post where post_state=0 order by id asc`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "获取已收藏数据成功!",
      data: results,
      total: results.length,
    });
  });
};
